
<?php get_main_menu("admin")?>
   <br/>
   <div style="width:90%; margin-left:auto; margin-right:auto;">
    <div class="form_title">

            <?php echo $_REQUEST["action"];?> Branch <?php echo $row_get_branch_by_id['name']?>
    </div>
    <div class="box">

<?php
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "branch_form") && isset($_REQUEST['save'])) {
  $insertSQL = sprintf("INSERT INTO location (id, plot_no, town, village, created_by, created_date, record_status) VALUES (%s, %s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['id'], "int"),
                       GetSQLValueString($_POST['plot_no'], "text"),
                       GetSQLValueString($_POST['town'], "text"),
                       GetSQLValueString($_POST['village'], "text"),
                       GetSQLValueString($loggedin_id, "int"),
                       GetSQLValueString(date("y-m-d H:i:s"), "date"),
                       GetSQLValueString($_POST['record_status'], "text"));
  mysql_select_db($database_drycleaners_conn, $drycleaners_conn);
  $Result1 = mysql_query($insertSQL, $drycleaners_conn) or die(mysql_error());
 
  if(mysql_affected_rows($drycleaners_conn)>0){
	  
mysql_select_db($database_drycleaners_conn, $drycleaners_conn);
$query_location = "SELECT * FROM location order by  `id`  desc LIMIT 1";
$location = mysql_query($query_location, $drycleaners_conn) or die(mysql_error());
$row_location = mysql_fetch_assoc($location);
$totalRows_location = mysql_num_rows($location);
	  echo $row_location["id"];
	    $qeury_branch = sprintf("INSERT INTO branch (name, location_id, created_by, created_date, record_status) VALUES (%s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['name'], "text"),
					   GetSQLValueString($row_location["id"], "int"),
                       GetSQLValueString($loggedin_id, "int"),
                       GetSQLValueString(date("y-m-d H:i:s"), "date"),
                       GetSQLValueString($_POST['record_status'], "text"));
  mysql_select_db($database_drycleaners_conn, $drycleaners_conn);
  $Result_branch = mysql_query($qeury_branch, $drycleaners_conn) or die(mysql_error());
  if(mysql_affected_rows($drycleaners_conn)>0){
	  redirect_drycleaners("branch_view","2");
	  }
	  }
}


$colname_get_branch_by_id = "-1";
if (isset($_GET['id'])) {
  $colname_get_branch_by_id = $_GET['id'];
}

mysql_select_db($database_drycleaners_conn, $drycleaners_conn);
$query_get_branch_by_id = sprintf("SELECT branch.id,location.plot_no,
       location.town,
       location.village,
	   branch.location_id,
       branch.name
  FROM    drycleaners.branch branch
       INNER JOIN
          drycleaners.location location
       ON (branch.location_id = location.id) WHERE branch.id = %s", GetSQLValueString($colname_get_branch_by_id, "int"));
$get_branch_by_id = mysql_query($query_get_branch_by_id, $drycleaners_conn) or die(mysql_error());
$row_get_branch_by_id = mysql_fetch_assoc($get_branch_by_id);
$totalRows_get_branch_by_id = mysql_num_rows($get_branch_by_id);
?>

 <?php 
 if(isset($_REQUEST["update"])){
			
			if(isset($_REQUEST["id"])){	
			mysql_select_db($database_drycleaners_conn, $drycleaners_conn);
			$query_update_user="update branch set name='".$_REQUEST['name']."'
			WHERE branch.id='".$_REQUEST["id"]."'";
			$results_user=mysql_query($query_update_user,$drycleaners_conn) or die(mysql_error());
			}
			
			if(isset($_REQUEST["location_id"])){
			mysql_select_db($database_drycleaners_conn, $drycleaners_conn);
			$query_update_contact="UPDATE location set plot_no='".$_REQUEST['plot_no']."', 
			town='".$_REQUEST['town']."',village='".$_REQUEST['village']."' WHERE location.id='".$_REQUEST["location_id"]."'";
			$results_contacts=mysql_query($query_update_contact,$drycleaners_conn) or die(mysql_error());
			}

			redirect_drycleaners("branch_view","2");

	 }
 ?> 
<form action="<?php echo $editFormAction; ?>" method="POST" name="branch_form" id="branch_form">
  <table align="left">

    <tr valign="baseline">
      <td nowrap="nowrap" align="left">Branch Name</td>
      <td><input type="text" name="name" value="<?php echo$row_get_branch_by_id['name'] ?>" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="left">Plot No:</td>
      <td><input type="text" name="plot_no" value="<?php echo$row_get_branch_by_id['plot_no'] ?>" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="left">Town:</td>
      <td><input type="text" name="town" value="<?php echo$row_get_branch_by_id['town'] ?>" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="left">Village:</td>
      <td><input type="text" name="village" value="<?php echo$row_get_branch_by_id['village'] ?>" size="32" /></td>
    </tr> 
   <tr valign="baseline">
      <td nowrap="nowrap" align="left">&nbsp;</td>
      <td>
       <?php if(isset($_REQUEST['branch_edit'])){	
		echo"<input name='update' type='submit' value='UPDATE' id='submit'/>";
		echo"<input type='hidden' name='MM_update' value='branch_form' />";	
		echo"&nbsp;&nbsp;<a href='.?branch_view'><input name='add' type='button' value='Cancel' id='submit'/></a>";
		}
		else{
			echo"<input name='save' type='submit' value='SAVE' id='submit'/>";
			echo"<input type='hidden' name='MM_insert' value='branch_form' />";	
			echo"&nbsp;&nbsp;<a href='.?branch_view'><input name='add' type='button' value='Cancel' id='submit'/></a>";
			}
		?>
      </td>
    </tr>
  </table>
  <input type="hidden" name="id" value="<?php echo$row_get_branch_by_id['id'] ?>" />
  <input type="hidden" name="location_id" value="<?php echo $row_get_branch_by_id['location_id'] ?>" />
  <input type="hidden" name="record_status" value="active" />
  <input type="hidden" name="MM_insert" value="form1" />
  <input type="hidden" name="MM_insert" value="branch_form" />
</form>
</div>
</div>
<?php
mysql_free_result($get_branch_by_id);
?>